package com.cs2340.practice;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class MapsOverlay extends com.google.android.maps.Overlay {

	GeoPoint gp;
	Map map;
	
	public MapsOverlay(Map map) {
		this.map = map;
		gp = map.getGP();
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when) {
	     super.draw(canvas, mapView, shadow);                   
	 
	     //translate the GeoPoint to screen pixels
	     Point screenPts = new Point();
	     mapView.getProjection().toPixels(map.getGP(), screenPts);
	 
	     //add the marker
	     Bitmap bmp = BitmapFactory.decodeResource(map.getResources(), R.drawable.pushpin);            
	     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
	     return true;
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
		//when user lifts his finger
        if (event.getAction() == 1 && (event.getEventTime()-event.getDownTime())>=5000) {
        	GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
        	String lat = p.getLatitudeE6()/ 1E6+"";
        	String lon = p.getLongitudeE6()/ 1E6 +"";
        	map.storeLocation(lat, lon);
        }
        return false;
    }//if(prev.getAction()==0){
}
